/* Copyright 2015 David Overeem (dovereem@cox.net)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.xtce.apps.editor.dialogs;

import java.awt.event.WindowEvent;
import java.util.List;
import javax.swing.DefaultListModel;
import org.xtce.apps.editor.ui.XTCEViewer;
import org.xtce.toolkit.XTCEDatabase;
import org.xtce.toolkit.XTCEFunctions;
import org.xtce.toolkit.XTCEParameter;
import org.xtce.toolkit.XTCETMContainer;

/**
 *
 * @author dovereem
 */

public class XTCEViewerParameterUsageDialog extends javax.swing.JFrame {

    /** This dialog box shows the containers that use a provided Parameter
     * from the XTCE document.
     *
     * @param parent XTCEViewer application for setting the initial location
     * of the dialog box.
     *
     * @param xtcedb XTCEDatabase object to perform the query against.
     *
     * @param parameter XTCEParameter object to search containers for.
     *
     */
    @SuppressWarnings("unchecked")
    public XTCEViewerParameterUsageDialog( XTCEViewer    parent,
                                           XTCEDatabase  xtcedb,
                                           XTCEParameter parameter ) {

        initComponents();

        xtceViewer_ = parent;
        containers_ = xtcedb.findContainers( parameter );

        DefaultListModel model = new DefaultListModel();

        for ( XTCETMContainer container : containers_ ) {
            model.addElement( container.getName() );
        }

        containerList.setModel( model );

        if ( containers_.size() > 0 ) {
            resultsText.setText( Long.toString( containers_.size() ) +
                                 " " + // NOI18N
                                 XTCEFunctions.getText( "dialog_findcontainersbyparameter_found" ) ); // NOI18N
        } else {
            resultsText.setText( XTCEFunctions.getText( "dialog_findcontainersbyparameter_none" ) + // NOI18N
                                 " '" + // NOI18N
                                 parameter.getName() +
                                 "'" ); // NOI18N
        }

        setLocationRelativeTo( xtceViewer_ );
        setVisible( true );

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        containerScrollPane = new javax.swing.JScrollPane();
        containerList = new javax.swing.JList<>();
        resultsText = new javax.swing.JLabel();
        buttonPanel = new javax.swing.JPanel();
        goToSelectedButton = new javax.swing.JButton();
        dismissButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/xtce/toolkit/MessagesBundle"); // NOI18N
        setTitle(bundle.getString("dialog_findcontainersbyparameter_wintitle")); // NOI18N

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(bundle.getString("dialog_findcontainersbyparameter_title")); // NOI18N

        containerScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        containerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        containerList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                containerListMouseClicked(evt);
            }
        });
        containerScrollPane.setViewportView(containerList);

        resultsText.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        resultsText.setText(bundle.getString("dialog_findparameter_initialresults_text")); // NOI18N

        goToSelectedButton.setText(bundle.getString("dialog_findparameter_goto_button")); // NOI18N
        goToSelectedButton.setMaximumSize(new java.awt.Dimension(125, 25));
        goToSelectedButton.setMinimumSize(new java.awt.Dimension(125, 25));
        goToSelectedButton.setPreferredSize(new java.awt.Dimension(125, 25));
        goToSelectedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToSelectedButtonActionPerformed(evt);
            }
        });

        dismissButton.setText(bundle.getString("dialog_findparameter_dismiss_button")); // NOI18N
        dismissButton.setMaximumSize(new java.awt.Dimension(125, 25));
        dismissButton.setMinimumSize(new java.awt.Dimension(125, 25));
        dismissButton.setPreferredSize(new java.awt.Dimension(125, 25));
        dismissButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dismissButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(buttonPanelLayout.createSequentialGroup()
                .addContainerGap(90, Short.MAX_VALUE)
                .addComponent(goToSelectedButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dismissButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(90, Short.MAX_VALUE))
        );
        buttonPanelLayout.setVerticalGroup(
            buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(goToSelectedButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dismissButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(resultsText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(containerScrollPane)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(containerScrollPane)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(resultsText)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void containerListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_containerListMouseClicked
        if ( evt.getClickCount() == 2 ) {
            goToSelectedButtonActionPerformed( null );
            dismissButtonActionPerformed( null );
        }
    }//GEN-LAST:event_containerListMouseClicked

    private void goToSelectedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goToSelectedButtonActionPerformed

        int row = containerList.getSelectedIndex();
        if ( row == -1 ) {
            return;
        }

        XTCETMContainer container = containers_.get( row );

        xtceViewer_.goToContainer( container.getName(),
                                   container.getSpaceSystemPath() );

    }//GEN-LAST:event_goToSelectedButtonActionPerformed

    private void dismissButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dismissButtonActionPerformed
        dispatchEvent( new WindowEvent( this, WindowEvent.WINDOW_CLOSING ) );
    }//GEN-LAST:event_dismissButtonActionPerformed

    // Private Member Variables

    private XTCEViewer            xtceViewer_ = null;
    private List<XTCETMContainer> containers_ = null;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JList<String> containerList;
    private javax.swing.JScrollPane containerScrollPane;
    private javax.swing.JButton dismissButton;
    private javax.swing.JButton goToSelectedButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel resultsText;
    // End of variables declaration//GEN-END:variables
}
